﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace HtmlAgilityPacknetcore
{
    public class ClassGenerator
    {
        public string GenerateClass(string namespaceName, string className, List<(string Type, string Name, string Summary)> properties, string classSummary)
        {
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Add using directives
            var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System"));
            compilationUnit = compilationUnit.AddUsings(usingDirective);

            // Define the namespace
            var namespaceDeclaration = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(namespaceName)).NormalizeWhitespace();

            // Define the class
            var classDeclaration = SyntaxFactory.ClassDeclaration(className)
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                .WithLeadingTrivia(CreateSummaryTrivia(classSummary)); // Add class summary

            // Add properties to the class
            foreach (var property in properties)
            {
                var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(property.Type), property.Name)
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                    .AddAccessorListAccessors(
                        SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                        SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)))
                    .WithLeadingTrivia(CreatePropertySummary(property.Summary)); // Add property summary

                classDeclaration = classDeclaration.AddMembers(propertyDeclaration);
            }

            // Add the class to the namespace
            namespaceDeclaration = namespaceDeclaration.AddMembers(classDeclaration);

            // Add the namespace to the compilation unit
            compilationUnit = compilationUnit.AddMembers(namespaceDeclaration);

            // Normalize and get code as string
            var code = compilationUnit.NormalizeWhitespace().ToFullString();
            return code;
        }

        private SyntaxTriviaList CreateSummaryTrivia(string summaryText)//类
        {
            var summaryContent = $"    /// <summary>\r\n    ///{summaryText} \r\n    /// </summary>\r\n    ";
            var summaryXml = SyntaxFactory.ParseLeadingTrivia(summaryContent);
            return SyntaxFactory.TriviaList(summaryXml);
        }

        private SyntaxTriviaList CreatePropertySummary(string summaryText)//属性
        {
            var summaryContent = $"    /// <summary>\r\n    ///{summaryText} \r\n    /// </summary>\r\n    ";
            var summaryXml = SyntaxFactory.ParseLeadingTrivia(summaryContent);
            return SyntaxFactory.TriviaList(summaryXml);
        }
    }
}
